package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.TypesCompositionsModel;
import application.model.modify.TypesCompositionModel;
import application.translators.Translator;
import application.translators.impl.TypesCompositionsTranslator;
import dao.TypesCompositionDao;
import mappings.TypesComposition;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("typesCompositions")
public class TypesCompositionsService
{
    private TypesCompositionDao typesCompositionDao = new TypesCompositionDao();
    private Translator<List<TypesComposition>, TypesCompositionsModel> translator = new TypesCompositionsTranslator();

    @Path("getForMaterial/{materialId}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public TypesCompositionsModel getTypesCompositionsForMaterial(@PathParam("materialId") int materialId)
    {
        return translator.translate(typesCompositionDao.getAllTypesCompositionsForMaterial(materialId));
    }

    @Path("addTypesComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public TypesCompositionModel addTypesComposition(@FormParam("materialId") int materialId,
                                                           @FormParam("typeId") int typeId)
    {
        TypesCompositionModel typesCompositionModel = new TypesCompositionModel();

        try
        {
            TypesComposition typesComposition = typesCompositionDao.addTypesComposition(materialId, typeId);
            typesCompositionModel.setId(typesComposition.getId());
            typesCompositionModel.setMaterialName(typesComposition.getMaterial().getName());
            typesCompositionModel.setTypeName(typesComposition.getTypes().getName());
            typesCompositionModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            typesCompositionModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            typesCompositionModel.setStatus(2);
        }

        return typesCompositionModel;
    }

    @Path("editTypesComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public TypesCompositionModel editTypesComposition(@FormParam("id") Integer id,
                                                  @FormParam("materialId") int materialId,
                                                  @FormParam("typeId") int typeId)
    {
        TypesCompositionModel typesCompositionModel = new TypesCompositionModel();

        try
        {
            TypesComposition typesComposition = typesCompositionDao.editTypesComposition(id, materialId, typeId);
            typesCompositionModel.setMaterialName(typesComposition.getMaterial().getName());
            typesCompositionModel.setTypeName(typesComposition.getTypes().getName());
            typesCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            typesCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            typesCompositionModel.setStatus(2);
        }

        return typesCompositionModel;
    }

    @Path("deleteTypesComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public TypesCompositionModel deleteTypesComposition(@FormParam("id") Integer id)
    {
        TypesCompositionModel typesCompositionModel = new TypesCompositionModel();

        try
        {
            typesCompositionDao.deleteTypesComposition(id);
            typesCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            typesCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            typesCompositionModel.setStatus(2);
        }

        return typesCompositionModel;
    }
}
